const StressManager = require('../stressManager');
const Thenjs = require('thenjs');
const logger = require('../lib/logger');

let limitArr = [100];
// let limitArr = [5];
const reqNum = 10000; // 请求数量
const limit = 10; // 并发数量
const httpClient = require('../node-client/httpClient');
const config = require('config');
const _ = require('lodash');


function main(callback) {
    logger.info('start ...');
    console.log('start ...');
    Thenjs(cont => {
        let taskStress = new StressManager('node-reset-client baidu', reqNum, limit, true);
        taskStress.stressMany(limitArr, getBaidu, cont);
    }).fin((cont, err, arg) => {
        return callback(err, arg);
    });
}

main((err, result) => {
    if (err) {
        console.error(err.stack || err);
        logger.error(err.stack || err);
        setTimeout(process.exit, 200, -1);
    } else {
        logger.info('success');
        console.log('success');
        setTimeout(process.exit, 200, 0);
    }
});

process.on('uncaughtException', err => {
    logger.error(`global exception: ${err.stack || err}`);
    console.error(`global exception: ${err.stack || err}`);
    setTimeout(process.exit, 200, -2);
});

function getBaidu(callback) {
    let method = 'get';
    let option = {
        headers: {},
        timeout: 20000,
    };
    let url = 'http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=DdzwVcsGMoYpeg5xQlAFrXQt';
    let start = new Date().getTime();
    httpClient.transport(method, url, {}, {}, option, function (err, result) {
        let endTime = Date.now();
        logger.info(`redirect time: ${endTime - start}`);
        console.log(`redirect time: ${endTime - start}`);
        return callback(null, result);
    });
}